import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../pages/home/Home'
import Login from '../pages/Login'
import City from '../pages/city/City'
import Detail from '../pages/detail/Detail'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../pages/home/Home.vue')
  },
  {
    path: '/city',
    name: 'City',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../pages/city/City.vue')
  },
  {
    path: '/detail',
    name: 'Detail',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../pages/detail/Detail.vue')
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// router.beforeEach((to, from, next) => {
//   if (to.path === '/') return next()
//   let tokenStr = window.sessionStorage.token
//   if (tokenStr) {
//     return next()
//   } else {
//     next('/')
//   }
// })
router.beforeEach((to, from, next) => {
  if (to.path === '/') return next()
  let tokenStr = window.sessionStorage.token
  if (tokenStr) {
    return next()
  } else {
    next('/')
  }
})

export default router
